﻿// mqtt.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include "mqtt.h"
#include "mqtt_server.h"
#include "mqtt_client.h"
#include "platform.h"

#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>


void client_test()
{
	MQTT* mqtt;
	mqtt = Create_mqtt(MQTT_CLIENT);
	MQTT_SetClientParameter(mqtt, "TEST", "TEST", "MQTTDESKTOP-55R6QNI", 1);
	printf("input ip:");
	scanf("%s", mqtt->IP);
	printf("input port:");
	scanf("%d", &mqtt->port);
	
	MQTTClient_Connect(mqtt, mqtt->IP, mqtt->port);
	MQTTClient_Subscribe(mqtt, "123qwer");
	MQTT_MSG msg;
	msg.Dup = 0;
	msg.len = strlen("hello,mqtt");
	msg.message = (char*)"hello,mqtt";
	msg.Qos = 1;
	while (1)
	{
		MQTT_Publish(mqtt, "123qwer", &msg);
		if (mqtt->Current_State == DISCONNECT)
		{
			mqtt_socket_thread_delete(mqtt);
			MQTTClient_Connect(mqtt, mqtt->IP, mqtt->port);
			MQTTClient_Subscribe(mqtt, "123qwer");
		}
		Sleep(10000);
	}
}
void server_test()
{	
	while (1)
	{
		mqtt_socket_thread_init(MQTT_SERVER, 0, 0, 0);		
	}
}
//0-SERVER 1-CLIENT
#define MQTT_TYPE 0
int main()
{
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

#if MQTT_TYPE	
	client_test();
#else
	server_test();
#endif
}